package com.web.service.Impl;

import com.web.dao.GateWayDao;
import com.web.exception.BusinessException;
import com.web.pojo.domain.GateWay;
import com.web.pojo.vo.GateWayList;
import com.web.service.GateWayServer;
import com.web.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: wws
 * @Date: 2021/1/9 12:14
 * describe:
 */
@Service
public class GateWayServerImpl implements GateWayServer {

    @Autowired
    GateWayDao gateWayDao;

    /**
     * 添加网关信息
     * @param gateWayInfo 网关信息
     * @return 结果
     */
    @Override
    public Integer insertGateWayInfo(GateWay gateWayInfo) {
        if (null == gateWayInfo.getGatewayName() || gateWayInfo.getGatewayName().equals("")) {
            throw new BusinessException("网关名不能为空");
        }
        Integer rows = gateWayDao.insertGateWay(gateWayInfo);
        Integer userId = SecurityUtils.getUserId();
        insertUserGateWay(userId, gateWayInfo.getGatewayId());
        return rows;
    }

    /**
     * 检查GateWayEui是否已经注册
     * @param gateWayEui 网关eui
     * @return 结果
     */
    @Override
    public Boolean checkGateWayEuiUnique(String gateWayEui) {
        Integer count = gateWayDao.checkGateWayEuiUnique(gateWayEui);
        return count > 0 ? false : true;
    }

    /**
     * 添加用户所拥有的控制的网关
     * @param userId 用户id
     * @param gateWayId 网关Id
     * @return 结果
     */
    @Override
    public Integer insertUserGateWay(Integer userId, Integer gateWayId) {
        return gateWayDao.insertUserGateWay(userId, gateWayId);
    }

    /**
     * 更新网关信息
     * @param gateWayInfo 网关信息
     * @return 结果
     */
    @Override
    public Integer updateGateWayInfo(GateWay gateWayInfo) {
        return gateWayDao.updateGateWayInfo(gateWayInfo);
    }

    /**
     * 删除网关信息
     * @param gateWayIds 网关id列表
     * @param updateBy 删除者Id
     * @return 结果
     */
    @Override
    public Integer deleteGateWayInfo(List<Integer> gateWayIds, String updateBy) {
        Integer result = gateWayDao.deleteGateWayInfo(gateWayIds, updateBy);
        gateWayDao.deleteUserGateWay(gateWayIds);
        return result;
    }

    /**
     * 查询网关列表
     * @param userId 用户id
     * @param str 查询条件
     * @return 结果
     */
    @Override
    public List<GateWayList> selectGateWayList(Integer userId, String str) {
        return gateWayDao.selectGateWayList(userId, str);
    }

    /**
     * 查看网关详细信息
     * @param gateWayId 网关id
     * @return 结果
     */
    @Override
    public GateWay selectGateWayDetails(Integer gateWayId) {
        return gateWayDao.selectGateWayDetails(gateWayId);
    }

    /**
     * 根据地址查询设备
     * @param userId 用户id
     * @param addressId 地址
     * @return 结果
     */
    @Override
    public List<GateWay> selectGateWayListByAddress(Integer userId, Integer addressId) {
        return gateWayDao.selectGateWayListByAddress(userId, addressId);
    }

    /**
     * 网关授权
     * @param ids 授权的Id
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public Integer authorizeGateWay(List<Integer> ids, Integer userId) {
        return gateWayDao.authorizeGateWay(ids,userId);
    }


}
